
import type { RouteRecordRaw } from 'vue-router';


let modules: any = import.meta.glob("./router/*/*.ts", { eager: true, import: 'default' });


let routerStartWith:string =  './router/static/';




// console.log(modules);



// * 导入所有静态router
const routerArray: Array<RouteRecordRaw> = [];


const initRouterData = (item: RouteRecordRaw): RouteRecordRaw => {

        // if (item.path.startsWith('/:city/')) item.path = item.path.substring(6);

        // else if (item.path === '/:city') item.path = '/';

        // if (item.path === '') item.path = '/';

        // if (item.meta && item.meta.crumbs) {

        //     const crumbsFunc = (path: string) => {
        //         return path.startsWith('/:city/') ? path.substring(6) : path;
        //     }

        //     if (item.meta.crumbs instanceof Array) {
        //         item.meta.crumbs = item.meta.crumbs.map((item) => {
        //             item.path = crumbsFunc(item.path);
        //             return item;
        //         });
        //     } else if (((item.meta.crumbs) as any).path) {
        //         ((item.meta.crumbs) as any).path = crumbsFunc(((item.meta.crumbs) as any).path);

        //     }
        // }

        // if (item.children) item.children = item.children.map((items) => initRouterData(items));
    
    //   item.component = () => {
    //     //根据组件路径动态获取组件实例
    //     const component = viewsComponent[componentPath]
    //     if (!component) throw new Error(`${componentPath} 组件文件不存在！`)
    //     //重新构造组件，调整组件name
    //     return component().then(comp => ({
    //         ...comp.default,
    //         name: componentName,
    //     }))
    // }
    return item;
}

Object.keys(modules).forEach((key: string) => {
    if(key.startsWith(routerStartWith)){
        let arr = modules[key] as Array<RouteRecordRaw>;
        for (let i = 0; i < arr.length; i++) routerArray.push(initRouterData(arr[i]));
    }
});



export default {
    routes: (_routes: any) => {
        return [
            ..._routes,
            
            ...routerArray
        ]
    }
}
